package socketinterface;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.List;

import com.vast.ui.ledpanel.views.ViewPart;

public class ClientController implements Runnable {
	public  ViewPart view;
	private String hostName = "127.0.0.1";
	private InetAddress hostAddress = null;
	private int         portNumber = 20000;
	public Socket           handlingSocket       = null;
	public boolean          isOutputStreamOpened = false;
	public DataOutputStream dataOutputStream = null;
	public static final short   endMarker = 0x7FFF;
	private Logger       logger;
	
	public ClientController(ViewPart view) {
		this.view = view;
		logger = new ConsoleLogger();
	}
	
	public void setHostName(String hostName) {
		this.hostName = hostName;
	}

	public String getHostName() {
		return hostName;
	}
	
	public void connectToServer() throws Exception {
		hostAddress = InetAddress.getByName(hostName);
		new Thread(this).start();
	}
	
	public void run() {
		// TODO Auto-generated method stub
		try {
			logger.writeEntry("Connect to " + hostName + "port: " + portNumber);
			handlingSocket = new Socket(hostAddress, portNumber);
			//dataOutputStream = new DataOutputStream(new BufferedOutputStream(handlingSocket.getOutputStream()));
			dataOutputStream = new DataOutputStream(handlingSocket.getOutputStream());
			isOutputStreamOpened   = true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			logger.writeEntry("Error :" + e.getMessage());
			e.printStackTrace();
			return;
		}
		logger.writeEntry("Connect to Servr successful");
		logger.writeEntry("DataOutputStream opened");
	}

	/*
	public void sendByteDataArray(byte command, ByteDataArray byteDataArray) {
		int sizeOfSendpacket = 3 + 8 + byteDataArray.cells.length;
		logger.writeEntry("Send ByteDataArray : Address :" + byteDataArray.address + "Total packet size :" + sizeOfSendpacket);
		try {
		dataOutputStream.writeShort(sizeOfSendpacket);
		dataOutputStream.writeByte(command);
		dataOutputStream.writeLong(byteDataArray.address);
		dataOutputStream.write(byteDataArray.cells);
		dataOutputStream.writeShort(endMarker);	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	*/
	
	public void send(byte command, byte[] data){		
		int sizeOfSendpacket;
		if ( data != null) {
			System.arraycopy(data, 0, data, 0, data.length);
			sizeOfSendpacket = (short)(3 + data.length);
		} else {
			sizeOfSendpacket = 3;
		}
		
		logger.writeEntry("  Packet Size: " + sizeOfSendpacket);
		logger.writeEntry("      Command: " + command);
		logger.writeEntry(" Size of Data: " + data.length);
		
		for (int i = 0; i <data.length; i++) {
			logger.writeEntry("data[" +i + "]:" + data[i]);
		}
		logger.writeEntry("EndMarker: " + endMarker);
		try {
			if (sizeOfSendpacket > 3) {
				dataOutputStream.writeShort(sizeOfSendpacket);
				dataOutputStream.writeByte(command);
				dataOutputStream.write(data);
				dataOutputStream.writeShort(endMarker);
			} else if (sizeOfSendpacket == 3) {
				dataOutputStream.writeShort(sizeOfSendpacket);
				dataOutputStream.writeShort(command);
				dataOutputStream.writeShort(endMarker);
			} else {
				
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setPortNumber(int portNumber) {
		this.portNumber = portNumber;
	}
	
	public void setPortNumber(String portNumber) {
		this.portNumber = Integer.parseInt(portNumber);
	}

	public int getPortNumber() {
		return portNumber;
	}
}
